﻿using Microsoft.EntityFrameworkCore;
using mozhi.smarterp.Finance.EntityFrameworkCore;
using mozhi.smarterp.Finance.Files.Repository;
using mozhi.smarterp.Finance.Querys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace mozhi.smarterp.Finance.Files
{
    public class AttachmentRepository : EfCoreRepository<FinanceDbContext, Attachment, Guid>, IAttachmentRepository
    {
        public AttachmentRepository(IDbContextProvider<FinanceDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        public async Task<List<Attachment>> FindAsync(AttachmentPageQuery query)
        {
            var attaments = await DbContext.Set<Attachment>().AsNoTracking().
                WhereIf(!query.ModuleCode.IsNullOrEmpty(), x => x.ModuleCode == query.ModuleCode).
                WhereIf(query.OwnerId.HasValue, x => x.OwnerId == query.OwnerId).
                OrderBy(x => x.CreationTime).
                Skip((query.PageIndex - 1) * query.PageSize).Take(query.PageSize).
                ToListAsync();

            return attaments;

        }

        public async Task<int> GetCountAsync(AttachmentPageQuery query)
        {
            var count = await DbContext.Set<Attachment>().AsNoTracking().
                WhereIf(!query.ModuleCode.IsNullOrEmpty(), x => x.ModuleCode == query.ModuleCode).
                WhereIf(query.OwnerId.HasValue, x => x.OwnerId == query.OwnerId).
                OrderBy(x => x.CreationTime).
                CountAsync();

            return count;
        }
    }
}
